package com.xframe.xdal.core.manage;

import cn.hutool.db.handler.HandleHelper;
import com.xframe.xdal.core.handler.ResultSetHandler;
import lombok.SneakyThrows;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ResultHandler<E> implements ResultSetHandler<List<E>> {
    /**
     * 类型
     */
    private Class<E> elementBeanType;

    /**
     * 根据类型获取处理起
     * @param beanType 类型
     * @param <E> 泛型
     * @return
     */
    public static <E> ResultHandler<E> create(Class<E> beanType) {
        return new ResultHandler(beanType);
    }

    public ResultHandler(Class<E> beanType) {
        this.elementBeanType = beanType;
    }

    @SneakyThrows
    public List<E> handle(ResultSet resultSet) {
        return (List) HandleHelper.handleRsToBeanList(resultSet, new ArrayList(), this.elementBeanType);
    }
}
